<?php

namespace app\controllers\h5\uni;

use app\controllers\h5\base\H5Controller;
use app\models\merchant\MerchantType;
use app\redis\Cache;

/**
 * Class MerchantController
 * @package app\controllers\h5\uni
 */
class MerchantController extends H5Controller
{
    public function actionIndex()
    {
        $cache = Cache::get(Cache::$PAGE_MERCHANT);

        if (!$cache) {
            $data = [
                'type' => $this->type(),
            ];

            Cache::set(Cache::$PAGE_MERCHANT, json_encode($data));
        } else {
            $data = json_decode($cache, true);
        }

        return $this->successEncode($data);
    }

    private function type()
    {
        return MerchantType::find()
            ->where(['status' => MerchantType::$statusMapping['OK']])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
            ->with(['image'])
            ->asArray()
            ->all();
    }
}